<?php

require '../db.php';

$ip = getUserIP();

if (isset($_POST['id'])) {
    $id = $_POST['id'];

    $query = "select ip from tbl_vote where pro_id = $id and ip = '$ip'";
    $result = connect($query);

    $count = mysql_num_rows($result);

    if ($count == 0) {
        $query = "update tbl_products set thumb_down = thumb_down + 1 where pro_id = $id";
        connect($query);

        $query = "insert into tbl_vote(pro_id, ip) values($id, '$ip')";
        connect($query);

        echo "<script>document.getElementById('mess').innerHTML='Cảm ơn bạn đã vote!';</script>";
    } else {
        echo "<script>document.getElementById('mess').innerHTML='Bạn đã vote rồi!';</script>";
    }

    $query = "select thumb_down from tbl_products where pro_id = $id";
    $result = connect($query);
    $row = mysql_fetch_array($result);
    $down = $row['thumb_down'];
    echo $down;
}
